package com.shushan.mixedexam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.android.net.util.NetToastUtil;
import com.shushan.login.bean.ChildBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.pages.OnOutlineSelectListener;
import com.shushan.pages.bean.OutlineBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.DensityUtil;
import com.shushan.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: StudyProcessSelectAdapter
 * @Desciption: 学习进度选择adapter
 * @author: yichaohua
 * @date: 2018-01-31
 */
public class StudyProcessSelectAdapter extends BaseAdapter{

    private static final String TAG = "StudyProcessSelectAdapter";

    private Context context;
    private OnOutlineSelectListener onOutlineSelectListener;
    private List<OutlineBean> outlineList = new ArrayList<OutlineBean>();
    private BabyApplication babyApplication = BabyApplication.getInstance();
    private boolean isOc;

    private int selectIndex = -1;
    private int selectId;
    private int subjectId;
    private int outlineId;
    private int practiceType;

    public StudyProcessSelectAdapter(Context context, int subjectId, int outlineId, List<OutlineBean> childOutlineList) {
        this.context = context;
        this.subjectId = subjectId;
        this.outlineId = outlineId;

        for (OutlineBean childOutlineBean : childOutlineList){
            outlineList.add(childOutlineBean);

            if(childOutlineBean.getOutlineList().size() > 0){
                childOutlineBean.getLeafList(outlineList);
            }

        }

    }

    public StudyProcessSelectAdapter(Context context, int subjectId, int outlineId, List<OutlineBean> childOutlineList,boolean isOc) {
        this.context = context;
        this.subjectId = subjectId;
        this.outlineId = outlineId;
        this.isOc = isOc;

        for (OutlineBean childOutlineBean : childOutlineList){
            outlineList.add(childOutlineBean);

            if(childOutlineBean.getOutlineList().size() > 0){
                childOutlineBean.getLeafList(outlineList);
            }

        }

    }



    @Override
    public int getCount() {
        return outlineList.size();
    }

    @Override
    public Object getItem(int position) {
        return outlineList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final OutlineBean childOutlineBean = outlineList.get(position);

        View view = (View) LayoutInflater.from(context).inflate(R.layout.study_process_select_item_text, null);
        ImageView lockImg = view.findViewById(R.id.study_process_title_item_lock_img);
        TextView scoreTextView = view.findViewById(R.id.study_process_score_item_tv);
        TextView titleTextView = view.findViewById(R.id.study_process_title_item_tv);
        titleTextView.setText(childOutlineBean.outline_name);
        if(childOutlineBean.score >= 0){
            if(isOc){
                scoreTextView.setVisibility(View.GONE);
            }else {
                scoreTextView.setText((childOutlineBean.score / 100) + "分");
            }
        }

        if((selectIndex < 0 && (selectId == childOutlineBean.id)) || (selectIndex == position)){
            titleTextView.setTextColor(context.getResources().getColor(R.color.primary_green_color));
        }else {
            titleTextView.setTextColor(context.getResources().getColor(R.color.deep_black_text_color));
        }

        view.setPadding(DensityUtil.dip2px(context,20)*(childOutlineBean.chilidLevel-1),
                DensityUtil.dip2px(context,14),0,DensityUtil.dip2px(context,14));

        if(childOutlineBean.getOutlineList().size() > 0){
            view.setOnClickListener(null);
        }else {
            if(!(babyApplication.isSectVip(practiceType) || childOutlineBean.state == 0)){
                lockImg.setVisibility(View.VISIBLE);
            }else {
                lockImg.setVisibility(View.INVISIBLE);
            }

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(!(babyApplication.isSectVip(practiceType) || childOutlineBean.state == 0)){
                        ToastUtils.showToast(context,"需要购买vip");
                    }else {
                        setNetOutline(position,childOutlineBean);
                    }

                }
            });
        }

        return view;
    }

    public int getOutlineId(){
        if(selectIndex < 0){
            return -1;
        }

        return outlineList.get(selectIndex).id;
    }

    public int getSelectId() {
        return selectId;
    }

    public void setSelectId(int selectId) {
        this.selectId = selectId;
    }

    private void setNetOutline(final int position,OutlineBean childOutlineBean) {
        ChildBean currentChildBean = BabyApplication.getInstance().getCurrentChildBean();
        if (currentChildBean == null) {
            return;
        }
        if(isOc){
            UrlParse urlParse = new UrlCommParse(context,UrlConst.BASE_URL).appendRegion(UrlConst.OC_LIST_PROGRESS_SET);
            UrlParse paramParse = new UrlCommParse(context)
                    .putValue("outline_id",outlineId)
                    .putValue("child_id",currentChildBean.id)
                    .putValue("outline_detail_id",childOutlineBean.id);
            NetHelper.create()
                    .url(urlParse.toStringOnlyHeader())
                    .param(paramParse.toStringOnlyParam())
                    .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                        @Override
                        protected void onSuccess(String data) {
                            selectIndex = position;
                            notifyDataSetChanged();

                            if (onOutlineSelectListener != null) {
                                onOutlineSelectListener.onOutlineSelected(100);
                            }
                        }

                        @Override
                        protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                            NetToastUtil.requestError(context, netRetBean);
                        }
                    });
        }else {
            UrlParse urlParse = new UrlCommParse(context, UrlConst.BASE_URL).appendRegion(UrlConst.SET_SELECT_PROGRESS);
            UrlParse paramParse = new UrlCommParse(context)
                    .putValue("child_id", currentChildBean.id)
                    .putValue("outline_id", outlineId)
                    .putValue("outline_detail_id", childOutlineBean.id)
                    .putValue("grade_id", currentChildBean.gradeId)
                    .putValue("subject_id", subjectId)
                    .putValue("semester", currentChildBean.semester)
                    .putValue("practice_type", practiceType);

            NetHelper.create()
                    .url(urlParse.toStringOnlyHeader())
                    .param(paramParse.toStringOnlyParam())
                    .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {

                        @Override
                        protected void onSuccess(String data) {
                            selectIndex = position;
                            notifyDataSetChanged();

                            if (onOutlineSelectListener != null) {
                                onOutlineSelectListener.onOutlineSelected(100);
                            }
                        }

                        @Override
                        protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                            NetToastUtil.requestError(context, netRetBean);

                        }
                    });
        }
    }

    public void setOnOutlineSelectListener(OnOutlineSelectListener onOutlineSelectListener) {
        this.onOutlineSelectListener = onOutlineSelectListener;
    }

    public int getPracticeType() {
        return practiceType;
    }

    public void setPracticeType(int practiceType) {
        this.practiceType = practiceType;
    }

}
